<?php


namespace app\mall\controller\Order\Collage;


use app\mall\controller\AppController;
use app\mall\model\Order\OrderModel;
use app\api\model\Collage\CollageModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class Collage extends AppController
{

    /**
     * 分页查询拼团
     * @param Request $request
     * @return array
     */
    public function getCollageGoodsOrderList(Request $request)
    {
        $param = $request->param();
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $where = [];

        if (!empty($param['keyword'])) {
            $where['collage_sn'] = ['like', "%" . $param['keyword'] . "%"];
        }
        array_push($where, ['mer_id', '=', $this->mall['id']]); //商家ID
        $list = CollageModel::getCollageListWherePage($page, $limit, $where);
        foreach ($list as $item) {
            $item['order_list'] = OrderModel::getOrderWhereMore([['collage_id', '=', $item['id']]]);
            foreach ($item['order_list'] as &$value) {
                foreach ($value['list'] as &$val) {
                    $goods_info = json_decode($val['goods_info'], true);
                    $val['goods_info'] = $goods_info;
                    if ($val['type'] == 1) {//商品
                        $data = [
                            'name' => $goods_info['good_name'],
                            'img' => ImageUtil::i()->replaceUrl($goods_info['img']),
                            'number' => $val['number'],
                            'slider_img' => $goods_info['slider_img'],
                            'price' => $val['goods_price'],
                            'total_price' => $val['total_price']
                        ];
                    }
                    if ($val['type'] == 2) {//会员
                        $data = [
                            'name' => $goods_info['level_name'],
                            'img' => ImageUtil::i()->replaceUrl($goods_info['icon']),
                            'number' => $val['number'],
                            'slider_img' => "",
                            'price' => $val['goods_price'],
                            'total_price' => $val['total_price']
                        ];
                    }
                    if ($val['type'] == 3) {//课程
                        $data = [
                            'name' => $goods_info['title'],
                            'img' => ImageUtil::i()->replaceUrl($goods_info['img']),
                            'number' => $val['number'],
                            'slider_img' => $goods_info['slider_img'],
                            'price' => $val['goods_price'],
                            'total_price' => $val['total_price']
                        ];
                    }
                    $val['info'] = !empty($data) ? $data : [];
                }
                unset($val);
            }
        }
        $count = CollageModel::getCollageListWhereCount($where);
        return Util::returnPageJson(self::CODE_DATA, $count, $list);
    }

    /**
     * 拼团订单
     * @param Request $request
     * @return array
     */
    public function getOrderGroupGoodsList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $status = !empty($param['status']) ? $param['status'] : "";
        if (!empty($status)) {
            array_push($where, ['o.status', '=', $status]);
        }
        if (!empty($param['collage_id'])) {
            array_push($where, ['o.collage_id', '=', $param['collage_id']]);
        }
        if ($status == 1 || $status == 3) {
            array_push($where, ['c.status', '=', 1]);
        }
        $list = OrderModel::getOrderListWhereJoin($page, $limit, $where);
        foreach ($list as &$item) {
            foreach ($item['list'] as &$val) {
                $goods_info = json_decode($val['goods_info'], true);
                $val['goods_info'] = $goods_info;
                if ($val['type'] == 1) {//商品
                    $data = [
                        'name' => $goods_info['good_name'],
                        'img' => ImageUtil::i()->replaceUrl($goods_info['img']),
                        'number' => $val['number'],
                        'slider_img' => $goods_info['slider_img'],
                        'price' => $val['goods_price'],
                        'total_price' => $val['total_price']
                    ];
                }
                if ($val['type'] == 2) {//会员
                    $data = [
                        'name' => $goods_info['level_name'],
                        'img' => ImageUtil::i()->replaceUrl($goods_info['icon']),
                        'number' => $val['number'],
                        'slider_img' => "",
                        'price' => $val['goods_price'],
                        'total_price' => $val['total_price']
                    ];
                }
                if ($val['type'] == 3) {//课程
                    $data = [
                        'name' => $goods_info['title'],
                        'img' => ImageUtil::i()->replaceUrl($goods_info['img']),
                        'number' => $val['number'],
                        'slider_img' => $goods_info['slider_img'],
                        'price' => $val['goods_price'],
                        'total_price' => $val['total_price']
                    ];
                }
                $val['info'] = !empty($data) ? $data : [];
            }
            unset($val);
        }
        $count = OrderModel::getOrderCountWhereJoin($where);
        return Util::returnPageJson(self::CODE_DATA, $count, $list);
    }

}